package com.ruoyi.web.controller.datamanagement;

import java.util.Iterator;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.PagePlus;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.PageUtils;
import com.ruoyi.common.utils.StringUtils;
import org.apache.commons.lang3.ArrayUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.datamanagement.domain.SysSearchTree;
import com.ruoyi.datamanagement.service.ISysSearchTreeService;
import com.ruoyi.common.utils.poi.ExcelUtil;

/**
 * 查询树Controller
 *
 * @author anan
 * @date 2021-03-31
 */
@RestController
@RequestMapping("/datamanagement/tree")
public class SysSearchTreeController extends BaseController
{
    @Autowired
    private ISysSearchTreeService searchTreeService;

    /**
     * 查询查询树列表
     */
    @PreAuthorize("@ss.hasPermi('datamanagement:searchTree:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysSearchTree sysSearchTree)
    {
		PagePlus<SysSearchTree, SysSearchTree> sysSearchTreeSysSearchTreePagePlus
			= searchTreeService.pageVo(PageUtils.buildPagePlus(), SysSearchTree.class);
		return PageUtils.buildDataInfo(sysSearchTreeSysSearchTreePagePlus);
    }

    /**
     * 导出查询树列表
     */
    @PreAuthorize("@ss.hasPermi('datamanagement:searchTree:export')")
    @Log(title = "查询树", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(SysSearchTree sysSearchTree)
    {
        List<SysSearchTree> list = searchTreeService.selectSysSearchTreeList(sysSearchTree);
        ExcelUtil<SysSearchTree> util = new ExcelUtil<SysSearchTree>(SysSearchTree.class);
        return util.exportExcel(list, "tree");
    }

    /**
     * 获取查询树详细信息
     */
    @PreAuthorize("@ss.hasPermi('datamanagement:searchTree:query')")
    @GetMapping(value = "/{searchTreeId}")
    public AjaxResult getInfo(@PathVariable("searchTreeId") Long searchTreeId)
    {
        return AjaxResult.success(searchTreeService.getById(searchTreeId));
    }

    /**
     * 新增查询树
     */
    @PreAuthorize("@ss.hasPermi('datamanagement:searchTree:add')")
    @Log(title = "查询树", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysSearchTree searchTree)
    {
        return toAjax(searchTreeService.save(searchTree));
    }

    /**
     * 修改查询树
     */
    @PreAuthorize("@ss.hasPermi('datamanagement:searchTree:edit')")
    @Log(title = "查询树", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysSearchTree searchTree)
    {
        return toAjax(searchTreeService.updateById(searchTree));
    }

    /**
     * 删除查询树
     */
    @PreAuthorize("@ss.hasPermi('datamanagement:searchTree:remove')")
    @Log(title = "查询树", businessType = BusinessType.DELETE)
	@DeleteMapping("/{searchTreeIds}")
    public AjaxResult remove(@PathVariable Long[] searchTreeIds)
    {
		List list = CollectionUtils.arrayToList(searchTreeIds);
        return toAjax(searchTreeService.removeByIds(list));
    }

    /**
     * 获取部门下拉树列表
     */
    @GetMapping("/treeselect")
    @PreAuthorize("@ss.hasPermi('datamanagement:searchTree:list')")
    public AjaxResult treeselect(SysSearchTree searchTree)
    {
		List<SysSearchTree> searchTrees = searchTreeService.list();
        return AjaxResult.success(searchTreeService.buildSearchTreeSelect(searchTrees));
    }

    /**
     * 查询部门列表（排除节点）
     */
    @PreAuthorize("@ss.hasPermi('datamanagement:searchTree:list')")
    @GetMapping("/exclude/{searchTreeId}")
    public AjaxResult excludeChild(@PathVariable(value = "searchTreeId", required = false) Long searchTreeId)
    {
        List<SysSearchTree> searchTrees = searchTreeService.list();
        Iterator<SysSearchTree> it = searchTrees.iterator();
        while (it.hasNext())
        {
            SysSearchTree d = (SysSearchTree) it.next();
            if (d.getSearchTreeId().intValue() == searchTreeId
                    || ArrayUtils.contains(StringUtils.split(d.getAncestors(), ","), searchTreeId + ""))
            {
                it.remove();
            }
        }
        return AjaxResult.success(searchTrees);
    }
}
